﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YLServerInterface.ServerConfig;
using System.Xml.Linq;

namespace YLServerInterface.Forms
{
    /// <summary>
    /// Interaction logic for EnvSetting.xaml
    /// </summary>
    public partial class EnvSetting : Window
    {
        public EnvSetting()
        {
            InitializeComponent();
            Initi();
        }
        YLServerConfig config = YLServerConfig.GetConfig();
        void Initi()
        {
            #region 自动生成
            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "WorkItemsFolder";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtWorkItemsFolder";
                txt.Text = config.WorkItemsFolder;
                txt.TextChanged += (sender, e) =>
                {
                    config.WorkItemsFolder = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "TemplateFolder";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtTemplateFolder";
                txt.Text = config.TemplateFolder;
                txt.TextChanged += (sender, e) =>
                {
                    config.TemplateFolder = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiExePath";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiExePath";
                txt.Text = config.YaZhiExePath;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiExePath = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiPrepareMakeTime";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiPrepareMakeTime";
                txt.Text = config.YaZhiPrepareMakeTime;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiPrepareMakeTime = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiMakeBtnName";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiMakeBtnName";
                txt.Text = config.YaZhiMakeBtnName;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiMakeBtnName = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiDialogBoxYesBtn";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiDialogBoxYesBtn";
                txt.Text = config.YaZhiDialogBoxYesBtn;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiDialogBoxYesBtn = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiRenderSelftTime";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiRenderSelftTime";
                txt.Text = config.YaZhiRenderSelftTime;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiRenderSelftTime = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }

            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "YaZhiDialogBoxRenderSelftTime";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtYaZhiDialogBoxRenderSelftTime";
                txt.Text = config.YaZhiDialogBoxRenderSelftTime;
                txt.TextChanged += (sender, e) =>
                {
                    config.YaZhiDialogBoxRenderSelftTime = txt.Text;
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }
            #endregion
            {
                var stackPnl = new StackPanel();
                var lbl = new Label();
                lbl.Content = "服务器地址";
                stackPnl.Children.Add(lbl);
                var txt = new TextBox();
                txt.Name = "txtServiceAddress";
                txt.Text = getServiceAddress();
                txt.TextChanged += (sender, e) =>
                {
                    setServiceAddress(txt.Text);
                };
                stackPnl.Children.Add(txt);
                stkPnl.Children.Add(stackPnl);
            }
        }
        #region 服务地址帮助方法
        private static string configPath = string.Empty;
        private static string ConfigPath
        {
            get
            {
                if (configPath == string.Empty)
                {
                    var file = Environment.CurrentDirectory;
                    var dir = Common.getDir(file);
                    configPath = dir + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe.config";
                }
                return configPath;
            }
        }
        private static string getServiceAddress()
        {
            var configDoc = XDocument.Load(ConfigPath);
            var address =
                configDoc.Element("configuration")
                .Element("system.serviceModel")
                .Element("client")
                .Element("endpoint")
                .Attribute("address");
            return address.Value;
        }
        private static void setServiceAddress(string value)
        {
            var configDoc = XDocument.Load(ConfigPath);
            var address =
                configDoc.Element("configuration")
                .Element("system.serviceModel")
                .Element("client")
                .Element("endpoint")
                .Attribute("address");
            address.Value = value;
            configDoc.Save(ConfigPath);
            YLServiceCLient.YLAdminClient.AdminServiceClient.ResetServiceSetting();
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            config.UpdateConfig();
        }
    }
}
